package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.MediaDTO;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.api.media.model.vo.PreviewMedia;
import com.xuecheng.media.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author by it-song
 * @Date 2021/12/15
 * @Description
 */
@RestController
public class MediaController implements MediaApi {
    @Autowired
    private MediaService mediaService;


    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {
        //获取机构的ID值
        Long companyId = SecurityUtil.getCompanyId();
        //2.将vo数据转换为dto数据  转换工具类
        MediaDTO dto = MediaConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);
        //3 调用service获得结果数据
        MediaDTO result = mediaService.createMedia(dto);
        return result;
    }


    @PostMapping("media/list")
    public PreviewMedia previewMediaList(PageRequestParams params, @RequestBody MediaVO mediaVO) {
        //获取公司ID
        Long companyId = SecurityUtil.getCompanyId();

        return mediaService.previewMediaList(params, mediaVO, companyId);

    }


    @GetMapping("media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();

        String VODUrl = mediaService.getVODUrl(mediaId, companyId);
        return VODUrl;
    }


    @GetMapping("media/{mediaId}")
    public Boolean deleteById(@PathVariable String mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        Boolean b=mediaService.deleteById(mediaId,companyId);
        return b;
    }

}
